/*
 *Se encarga de pintar en el frame la matriz Space
 *
 */
package spaceinvaders;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 *
 * @author jorge
 */
public class JSpace extends JPanel implements ActionListener
{

    private ArrayList<Pixel> pixels;
    private int[][] matriz;
    private Space space;
    private Timer timer;
    private Timer timer2;
    private ActionListener at;

    public JSpace(Space space)
    {
        super();
        super.setSize(600, 735);
        super.setBackground(Color.BLACK);

        this.pixels = new ArrayList<Pixel>();
        this.space = space;
        this.matriz = this.space.getSpace();
        this.timer = new Timer(500, this);
        this.timer2 = new Timer(10, this);

        this.timer.start();
        this.timer2.start();

        //crearPixel(3);

        super.setVisible(true);
    }

    /**
     * dibuja los pixeles en el frame, segun estan dispustos en la matriz de space, los pixeles estan el arraylist piexeles.
     * @param g Graphics del metodo paint
     */
    public void paintPixel(Graphics g)
    {
        crearPixel(3);

        Graphics2D g2 = (Graphics2D) g;
        int leng = this.pixels.size();
        Pixel pixel;
        Shape sh;


        for (int i = 1; i < leng; i++)
        {
            pixel = this.pixels.get(i - 1);

            sh = new Rectangle2D.Double(pixel.getCoordenadaX(), pixel.getCoordenadaY(), pixel.getTamano(), pixel.getTamano());

            g2.setColor(pixel.getColor());
            g2.fill(sh);

        }
    }

    /*
     * crea los pixeless segun estan en la matriz space
     */
    public void crearPixel(int tamano)
    {
        this.pixels.clear();

        for (int i = 0; i < 200; i++)
        {
            for (int j = 0; j < 245; j++)
            {
                switch (this.matriz[i][j])
                {
                    case 1:
                    {
                        this.pixels.add(new Pixel(j * tamano, i * tamano, tamano, Color.WHITE));
                        break;
                    }
                    case 2:
                    {
                        this.pixels.add(new Pixel(j * tamano, i * tamano, tamano, Color.BLUE));
                        break;
                    }
                    case 3:
                    {
                        this.pixels.add(new Pixel(j * tamano, i * tamano, tamano, Color.green));
                        break;
                    }
                    case 4:
                    {
                        this.pixels.add(new Pixel(j * tamano, i * tamano, tamano, Color.red));
                        break;
                    }
                    case 5:
                    {
                        this.pixels.add(new Pixel(j * tamano, i * tamano, tamano, Color.orange));
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
        }
    }

    @Override
    public void paint(Graphics g)
    {
        paintComponent(g);

        paintPixel(g);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == this.timer)
        {
            this.space.moverFiguras();
        }
        else if(e.getSource() == this.timer2)
        {
            this.space.moverTerricola();
            this.space.moverDisparo();
        }


        super.repaint();
    }
}
